package cn.tedu.hospital.admin.server.userlogin.account.controller;

import cn.tedu.hospital.admin.server.userlogin.account.pojo.param.UserLoginInfoParam;
import cn.tedu.hospital.admin.server.userlogin.account.pojo.vo.UserLoginResultVO;
import cn.tedu.hospital.admin.server.userlogin.account.service.IUserService;
import cn.tedu.hospital.admin.server.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 处理用户相关请求的控制器类
 *
 * @auther lester
 * @date 2023/6/30 15:37
 */
@Slf4j
@RestController
@RequestMapping("/users")
@Validated
@Api(tags = "1.1. 账号管理-用户管理")
public class UserController {

    @Autowired
    private IUserService userService;

    public UserController() {
        log.info("创建控制器对象：UserController");
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 10)// 操作显示顺序
    public JsonResult login(@Validated UserLoginInfoParam userLoginInfoParam) {
        log.debug("开始处理【用户登录】的请求，参数：{}",userLoginInfoParam);
        UserLoginResultVO userLoginResultVO = userService.login(userLoginInfoParam);
        return JsonResult.ok(userLoginResultVO);
    }

    @PostMapping("/logout")
    @ApiOperation("用户退出登录")
    @ApiOperationSupport(order = 11)
    public JsonResult logout() {
        log.debug("开始处理【用户退出登录】的请求，无参数");
        SecurityContextHolder.clearContext();
        return JsonResult.ok();
    }
}
